package com.zhang.shopcar.adapter;


import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhang.shopcar.R;
import com.zhang.shopcar.conf.Constants;
import com.zhang.shopcar.entity.GroupInfo;
import com.zhang.shopcar.entity.ProductInfo;
import com.zhang.shopcar.utils.UIUtils;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;


/**
 * ExpandableListView的购物车适配器
 *
 * @author chenjunsen
 *         2015年8月18日下午8:53:57
 */
public class ShopcartExpandableListViewAdapter extends BaseExpandableListAdapter {
    private List<GroupInfo> groups;
    private Map<String, List<ProductInfo>> children;
    private Context context;

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;
    private boolean mIsEditClick;

    /**
     * 构造函数
     *
     * @param groups   组元素列表
     * @param children 子元素列表
     * @param context
     */
    public ShopcartExpandableListViewAdapter(List<GroupInfo> groups, Map<String, List<ProductInfo>> children,
                                             Context context) {
        super();
        this.groups = groups;
        this.children = children;
        this.context = context;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String groupId = groups.get(groupPosition).getId();
        return children.get(groupId).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<ProductInfo> childs = children.get(groups.get(groupPosition).getId());
        return childs.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder gholder;
        if (convertView == null) {
            gholder = new GroupHolder();
            convertView = View.inflate(context, R.layout.item_shopcart_group, null);

            gholder.ll_shop = (LinearLayout) convertView.findViewById(R.id.ll_shop);
            gholder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
            gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(gholder);
        } else {
            gholder = (GroupHolder) convertView.getTag();
        }
        final GroupInfo group = (GroupInfo) getGroup(groupPosition);
        if (group != null) {
            gholder.tv_group_name.setText(group.getName());
            gholder.cb_check.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    group.setChoosed(((CheckBox) v).isChecked());
                    checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());//暴露组选接口
                }
            });
            gholder.cb_check.setChecked(group.isChoosed());
            gholder.ll_shop.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIUtils.showToast(UIUtils.getContext(),"进入店铺");

                   /* Intent intent = new Intent(UIUtils.getContext(), BussinessActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    UIUtils.mSp.putInt(Constants.SHOPID, Integer.parseInt(group.getId()));

                    UIUtils.getContext().startActivity(intent);*/
                    // UIUtils.showToast(UIUtils.getContext(),"进入店铺");


                }
            });

        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {


        final DecimalFormat df = new DecimalFormat("#0.00");
        final ChildHolder cholder;
        if (convertView == null) {
            cholder = new ChildHolder();
            convertView = View.inflate(context, R.layout.item_shopcart_product, null);
            cholder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);

            cholder.tv_product_name = (TextView) convertView.findViewById(R.id.tv_product_name);
            cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_product_desc);
            cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            cholder.iv_increase = (ImageView) convertView.findViewById(R.id.iv_increase);//加
            cholder.iv_decrease = (ImageView) convertView.findViewById(R.id.iv_decrease);//减
            cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_buy_count);
            cholder.iv_shop_pic = (ImageView) convertView.findViewById(R.id.iv_shop_pic);
            cholder.tv_shopcar_jifen = (TextView) convertView.findViewById(R.id.tv_shopcar_jifen);
            cholder.ll_good_details = (LinearLayout) convertView.findViewById(R.id.ll_good_details);

            convertView.setTag(cholder);
        } else {
            cholder = (ChildHolder) convertView.getTag();
        }
        final ProductInfo product = (ProductInfo) getChild(groupPosition, childPosition);
        if (product != null) {


            cholder.tv_product_name.setText(product.getName());
            cholder.tv_product_desc.setText(product.getDesc());
            String price = df.format(product.getPrice());
            cholder.tv_price.setText("￥" + price);
            int count = product.getCount();

            //设置加减号状态
            /* if (count <= 1) {
                cholder.iv_increase.setImageResource(R.mipmap.details_icon_addmini_nor);
                cholder.iv_decrease.setImageResource(R.mipmap.details_icon_reducemini_dis);
            } else if (count >= 99) {
                cholder.iv_increase.setImageResource(R.mipmap.details_icon_addmini_dis);
                cholder.iv_decrease.setImageResource(R.mipmap.details_icon_reducemini_nor);
            } else {
                cholder.iv_increase.setImageResource(R.mipmap.details_icon_addmini_nor);
                cholder.iv_decrease.setImageResource(R.mipmap.details_icon_reducemini_nor);
            }*/
            cholder.tv_count.setText(product.getCount() + "");
            cholder.tv_shopcar_jifen.setText(df.format(product.getPVValue()) + "积分");

            //图片加载
            String baseImageUrl = UIUtils.mSp.getString(Constants.BASEIMAGEURL, "");
            Picasso.with(UIUtils.getContext())
                    .load(baseImageUrl + product
                            .getImageUrl()).resize(UIUtils.dip2Px(250), UIUtils.dip2Px(250))
                    .centerCrop().into(cholder.iv_shop_pic);

            cholder.cb_check.setChecked(product.isChoosed());

            cholder.cb_check.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    product.setChoosed(((CheckBox) v).isChecked());

                    cholder.cb_check.setChecked(((CheckBox) v).isChecked());
                    checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());//暴露子选接口
                }
            });

                   /* product.setChooseShares(((CheckBox) v).isChecked());
                    cholder.cb_shopcar_gupiao.setChecked(((CheckBox) v).isChecked());
                    product.setChoosed(true);
                    checkInterface.checkChild(groupPosition, childPosition, true);//暴露子选接口
*/



            cholder.iv_increase.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count,
                            cholder.cb_check.isChecked());//暴露增加接口
                }
            });
            cholder.iv_decrease.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count,
                            cholder.cb_check.isChecked());//暴露删减接口
                }
            });

            cholder.ll_good_details.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
//                    进入商品详情
                   /* ProductInfo productInfo = children.get(groups.get(groupPosition).getId()).get(childPosition);
                    UIUtils.mSp.putInt(Constants.GOODSONLINEDETAILSID, (int) productInfo.getGoodsOnlineDetailsID());
                    long goodsOnLineId = productInfo.getGoodsOnlineId();
                    UIUtils.mSp.putLong(Constants.GOODSONLINEID, goodsOnLineId);
                    Intent intent = new Intent(mContext, CommodityActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);*/
                }
            });

        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 组元素绑定器
     *
     * @author chenjunsen
     *         2015年8月18日下午8:56:15
     */
    private class GroupHolder {
        CheckBox cb_check;
        TextView tv_group_name;
        LinearLayout ll_shop;
    }

    /**
     * 子元素绑定器
     *
     * @author chenjunsen
     *         2015年8月18日下午8:56:25
     */
    private class ChildHolder {
        ImageView iv_shop_pic;
        CheckBox cb_check;

        TextView tv_product_name;
        TextView tv_product_desc;
        TextView tv_price;
        ImageView iv_increase;
        TextView tv_count;
        ImageView iv_decrease;
        TextView tv_shopcar_jifen;
        LinearLayout ll_good_details;

    }

    /**
     * 复选框接口
     *
     * @author chenjunsen
     *         2015年8月18日下午8:56:39
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        public void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        public void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    /**
     * 改变数量的接口
     *
     * @author chenjunsen
     *         2015年8月18日下午8:56:50
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
    }
}
